package com.twu.biblioteca.model.menu.actions;

import com.twu.biblioteca.model.LibraryItem;
import com.twu.biblioteca.model.menu.Menu;
import com.twu.biblioteca.model.menu.MenuAction;

public class CheckoutAction extends MenuAction {

    private final Class itemType;

    public CheckoutAction(boolean needsLogin, Class itemType) {
        super(needsLogin);
        this.itemType = itemType;
    }

    @Override
    public boolean performAction(Menu menu, String[] args) {
        for (LibraryItem item : menu.getLibrary().getInventory()) {
            if (item.getClass() == itemType && !item.isCheckedOut() && item.hasDetails(args)) {
                item.setCheckedOut(true);
                menu.getLoggedInCustomer().getCheckedOutItems().add(item);
                return true;   //Don't Checkout all copies, just one
            }
        }
        return false;
    }
}
